package com.flyco.modules.stm.service.impl;

import com.flyco.modules.stm.entity.StmStoreMessage;
import com.flyco.modules.stm.entity.StmStoreMessageRe;
import com.flyco.modules.stm.entity.StmStoreMessageRead;
import com.flyco.modules.stm.mapper.StmStoreMessageReMapper;
import com.flyco.modules.stm.mapper.StmStoreMessageReadMapper;
import com.flyco.modules.stm.mapper.StmStoreMessageMapper;
import com.flyco.modules.stm.service.IStmStoreMessageService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 店铺通知表
 * @Author: flyco
 * @Date:   2022-10-09
 * @Version: V1.0
 */
@Service
public class StmStoreMessageServiceImpl extends ServiceImpl<StmStoreMessageMapper, StmStoreMessage> implements IStmStoreMessageService {

	@Autowired
	private StmStoreMessageMapper stmStoreMessageMapper;
	@Autowired
	private StmStoreMessageReMapper stmStoreMessageReMapper;
	@Autowired
	private StmStoreMessageReadMapper stmStoreMessageReadMapper;
	
	@Override
	@Transactional
	public void saveMain(StmStoreMessage stmStoreMessage, List<StmStoreMessageRe> stmStoreMessageReList,List<StmStoreMessageRead> stmStoreMessageReadList) {
		stmStoreMessageMapper.insert(stmStoreMessage);
		for(StmStoreMessageRe entity:stmStoreMessageReList) {
			//外键设置
			entity.setMessageId(stmStoreMessage.getId());
			stmStoreMessageReMapper.insert(entity);
		}
		for(StmStoreMessageRead entity:stmStoreMessageReadList) {
			//外键设置
			entity.setMessageId(stmStoreMessage.getId());
			stmStoreMessageReadMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(StmStoreMessage stmStoreMessage,List<StmStoreMessageRe> stmStoreMessageReList,List<StmStoreMessageRead> stmStoreMessageReadList) {
		stmStoreMessageMapper.updateById(stmStoreMessage);
		
		//1.先删除子表数据
		stmStoreMessageReMapper.deleteByMainId(stmStoreMessage.getId());
		stmStoreMessageReadMapper.deleteByMainId(stmStoreMessage.getId());
		
		//2.子表数据重新插入
		for(StmStoreMessageRe entity:stmStoreMessageReList) {
			//外键设置
			entity.setMessageId(stmStoreMessage.getId());
			stmStoreMessageReMapper.insert(entity);
		}
		for(StmStoreMessageRead entity:stmStoreMessageReadList) {
			//外键设置
			entity.setMessageId(stmStoreMessage.getId());
			stmStoreMessageReadMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		stmStoreMessageReMapper.deleteByMainId(id);
		stmStoreMessageReadMapper.deleteByMainId(id);
		stmStoreMessageMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			stmStoreMessageReMapper.deleteByMainId(Long.valueOf(id.toString()));
			stmStoreMessageReadMapper.deleteByMainId(Long.valueOf(id.toString()));
			stmStoreMessageMapper.deleteById(id);
		}
	}
	
}
